<%--
  Created by IntelliJ IDEA.
  User: 86181
  Date: 2022/11/14
  Time: 14:39
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>出库申请登记</title>
</head>
<body>
    <form id="Issuerequisitionform">
        <br>
        <div  class="row">
            <div class="col-md-offset-7">
                <input type="button" onclick="queryIssuerequisition()"  class="btn btn-primary" value="添加产品">
                <input type="reset"  class="btn btn-primary" value="清空">
                <input type="button" onclick="delatelssbtn()"  class="btn btn-danger" value="删除">
                <input type="submit"  class="btn btn-primary" value="确定申请">
            </div>
        </div>
        <br>
        <div  align="center" style="width:1100px;height:1000px;border:1px black solid;">
            <div class="row">
                <div><h2><strong>出库申请单</strong></h2></div>
            </div>
            <br>
            <div class="row">
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>出库人:</td>
                            <td><input required type="text" name="storer" style="outline: none;border: none;border-bottom:black 1px dashed"></td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>出库理由:</td>
                            <td>
                                <select name="reason" class="form-control">
                                    <option value="C002-2">赠送</option>
                                    <option value="C002-1">生产领料</option>
                                    <option value="C002-3">内部借领</option>
                                    <option value="C002-4">其他借领</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
            <br>
            <div style="height:33%;">
                <table id="Issuerequisitiontab">

                </table>
            </div>
            <div class="row">
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>总件数:</td>
                            <td><input readonly required type="text"  name="amountSum" id="lssuerequisitionamountSum" style="outline: none;border: none;"></td>
                        </tr>
                        <tr>
                            <td>登记人:</td>
                            <td><input type="text" required name="register" required style="outline: none;border:none;border-bottom:1px black dashed;"></td>
                        </tr>
                    </table>
                </div>
                <div class="col-md-6">
                    <table>
                        <tr>
                            <td>总金额:</td>
                            <td><input type="text" required id="lssuerequisitioncostPriceSum" name="costPriceSum" readonly style="outline: none;border: none"></td>
                        </tr>
                        <tr>
                            <td>登记时间:</td>
                            <td><input type="text" required name="registerTime" id="lssuerequisitionamountSumregisterTime" readonly style="outline: none;border: none;"></td>
                        </tr>
                    </table>
                </div>
            </div>
            <br>
            <div>
                <div class="form-group">
                    <label for="Issuerequisitiontext"  class="control-label">配置要求:</label>
                    <textarea class="form-control" name="remark" id="Issuerequisitiontext" style="width:800px;height:200px"></textarea>
                </div>
            </div>
        </div>
    </form>
        <!--查询所以的产品 和物料做添加模态框-->
        <div class="modal fade"  tabindex="-1" role="dialog" aria-labelledby="gridSystemModalLabel" id="Issuerequisitionmoda">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <button class="close" data-dismiss="modal">退出</button>
                    </div>
                    <div>
                        <table id="Issuerequisitionmodatab">

                        </table>
                    </div>
                </div>
            </div>
        </div>
        <script>
            //加载表格数据
            $(function () {
                //点击获取当前时间
                $.get("queryCellByDate.action",function (data) {
                    $("#lssuerequisitionamountSumregisterTime").val(data.datetime);
                },"json")

                //明细表格数据
                $("#Issuerequisitiontab").bootstrapTable({
                    sortable: true,//是否显示排序
                    sortOrder: "aso",//排序方式
                    sidePagination:"server", //分页方式：client客户端分页，server服务端分页（*）
                    showColumns: true,//是否显示所有的列（选择显示的列）
                    showRefresh: true,//是否显示刷新按钮
                    minimumCountColumns: 2,//最少允许的列数
                    uniqueId: "productname",//每一行的唯一标识，一般为主键列
                    showToggle: true,//是否显示详细视图和列表视图的切换按钮
                    cardView: false,//是否显示详细视图
                    detailView: false,//是否显示父子表,
                    queryParams:function (params) {
                        var queryallwarehousing={
                            pagesize:params.limit,
                            pageno:(params.offset/params.limit)+1,
                            sort:params.sort,
                            sortOrder:params.sort,
                        }
                        return queryallwarehousing;
                    },
                    columns:[
                        {
                            field:"",
                            title:"",
                            checkbox:true,
                        },
                        {
                            field:"productname",
                            title:"产品名称",

                        },
                        {
                            field:"productid",
                            title:"产品编号",
                        },
                        {
                            field:"productDescribe",
                            title:"描述",
                            formatter:function (value,row,index) {
                                return "<input type='text' value='全新' required style='outline: none;border: none;width:60px;background-color:seashell'>"
                            }
                        },{
                            field:"amount",
                            title:"数量",
                            formatter:function (value,row,index) {
                                return "<input type='number'  class='lssuerequisitionamount' required onchange='Issuerequisitionamount(this)'  style='outline: none;border: none;width:60px;background-color:seashell' value='"+value+"'>"
                            }
                        },
                        {
                            field:"amountUnit",
                            title:"单位",
                            formatter:function (value,row,index) {
                                return "<input type='text' value='件' required style='outline: none;border: none;width:60px;background-color:seashell'>"
                            }
                        },
                        {
                            field:"costprice",
                            title:"成本价(元)",
                        },
                        {
                            field:"subtotal",
                            title:"小计(元)",
                            formatter:function (value,row,index) {
                                return "<input type='number' required class='lssuereuisitionsubtotal' readonly style='outline: none;border: none;width:60px;background-color:seashell' value='"+value+"'>"
                            }
                        },

                    ]
                })
            })
            function queryIssuerequisition() {
                //显示模态框
                $("#Issuerequisitionmoda").modal("show");
                //模态框数据
                $("#Issuerequisitionmodatab").bootstrapTable({
                    contentType: "application/x-www-form-urlencoded", //如果用tomcat就要写这个
                    striped: true,//是否显示隔行变色
                    cache: false,//是否缓存
                    pagination: false,//是否显示分页
                    sortable: false,//是否显示排序
                    clickToSelect: true,//是否启用点击选中行
                    queryParams:function (params) {
                        var Issuerequisitionmodatab={
                            pagesize:params.limit,
                            pageno:(params.offset/params.limit)+1,
                            sort:params.sort,
                            sortOrder:params.sort,
                        }
                        return Issuerequisitionmodatab;
                    },
                    columns:[
                        {
                            field:"productid",
                            title:"产品编号",
                        },
                        {
                            field:"productname",
                            title:"产品名称",
                        },
                        {
                            field:"type",
                            title:"用途类型",
                            formatter:function (value,row,index) {
                                if(row.type=="Y001-1"){
                                    return "商品"
                                }
                                if(row.type=="Y001-2"){
                                    return "物料"
                                }
                            }
                        },
                        {
                            field:"productclass",
                            title:"档次级别",
                            formatter:function (value,row,index) {
                                if (row.productclass == "D001-1") {
                                    return "高档"
                                }
                                if (row.productclass == "D001-2") {
                                    return "中档"
                                }
                                if (row.productclass == "D001-3") {
                                    return "低档"
                                }
                            }
                        },
                        {
                            field:"personalunit",
                            title:"单位",
                        },
                        {
                            field:"costprice",
                            title:"成本单价",
                        },
                        {
                            field:"",
                            title:"出库",
                            formatter:function (value,row,index) {
                                return "<input type='button' onclick='Issuerequisitionproductid("+row.productid+")' value='出库' class='btn btn-primary'>"
                            }
                        },
                    ]
                })
                //查询所以的仓库里面有的产品
                $.post("queryByFileBychecktag.action",function (data) {
                    var arr=[];
                    var datas=data.rows;
                    for(var  i=0;i<datas.length;i++){
                        arr.push(datas[i])
                    }
                    $("#Issuerequisitionmodatab").bootstrapTable("load",arr)
                },"json")
            }

            function Issuerequisitionproductid(productid) {
                //获取当前id
                $.post("queryByProdutctid.action",{"productid":productid},function (data) {
                    var arr=new Array();
                    arr.push(data)

                    var bootstrapTable = $("#Issuerequisitiontab").bootstrapTable("getData");

                    if(bootstrapTable.length==0){
                        $("#Issuerequisitiontab").bootstrapTable("append",arr)
                    }else{
                        for(var i=0;bootstrapTable.length>i;i++){
                            if(data.productid==bootstrapTable[i].productid){
                                alert("你已添加过此产品");
                                return ;
                            }
                        }

                        $("#Issuerequisitiontab").bootstrapTable("append",arr)

                    }


                },"json")
            }
            //输入数量做改变事件
            function Issuerequisitionamount(amount) {
                //验证数字
                var reg=/^[0-9]*$/
                if(!reg.test($(amount).val())){
                    alert("请输入正确的数字")
                    return;
                }
                //当前数量数量
                var amounta=$(amount).val();
                //当前市场价
                var num=$(amount).parent().next().next().text();
                $(amount).parent().parent().find("td").eq(7).find("input").eq(0).val(amounta*num);

                //获取表格当前所有数据
                var arr2 =$("#Issuerequisitiontab").bootstrapTable("getData");
                var productid=$(amount).parent().parent().find("td").eq(2).text();
                var productDescribe=$(amount).parent().parent().find("td").eq(3).find("input").eq(0).val();
                //循环表格数据  和当前行id比较  相等  跟新num
                $(arr2).each(function (index,item) {
                    if(item.productid==productid){
                        item.amount = $(amount).val();
                        item.subtotal=amounta*num;
                        item.productDescribe=productDescribe;
                    }
                })
                //数据加载到表格
                $("#Issuerequisitiontab").bootstrapTable("load",arr2);
                //总件数
                var i=0;
                $(".lssuereuisitionsubtotal").each(function (index,item) {
                    i+=Number(item.value)
                })
                var i1=0;
                $(".lssuerequisitionamount").each(function (index,item) {
                    i1+=Number(item.value)
                })
                //总件数
                $("#lssuerequisitionamountSum").val(i1)
                //总金额
                $("#lssuerequisitioncostPriceSum").val(i)
            }
            //确定提交  添加出库明细 出库 数据
            $("#Issuerequisitionform").validate({
                submitHandler:function () {
                    //获取form的值
                   var arr=$("#Issuerequisitionform").serializeArray();
                   //创建出库表
                    var pay={}
                   $(arr).each(function (index,item) {
                        pay[item.name]=item.value;
                   })
                    //获取明细表数据
                    var payDetails=new Array();
                    pay["payDetails"]=payDetails
                    $("#Issuerequisitiontab tbody tr").each(function (index,n) {
                        payDetails.push({
                            productName:$(n).find("td").eq(1).text(),
                            productId:$(n).find("td").eq(2).text(),
                            productDescribe:$(n).find("td").eq(3).find("input").eq(0).val(),
                            amount:$(n).find("td").eq(4).find("input").eq(0).val(),
                            amountUnit:$(n).find("td").eq(5).find("input").eq(0).val(),
                            costPrice:$(n).find("td").eq(6).text(),
                            subtotal:$(n).find("td").eq(7).find("input").eq(0).val()})
                    })
                    $.ajax({
                        url:"addParByPayDetailds.action",
                        data:JSON.stringify(pay),
                        contentType:"application/json",
                        type:"post",
                        dataType:"json",
                        success:function (data) {
                            alert(data.msg)
                            $("#tab1").addTabs("出库申请登记审核", "", "examinelssuereqiosotion.jsp", true);
                            $("#tab1").closeTabs("出库申请登记")
                            $("#examinelssuereqiosotiontab").bootstrapTable("refresh")
                        }
                    })
                }
            })
            //删除checkbox
            function delatelssbtn() {
                  if(confirm("您确定要删除?")){
                      var cks=$("#Issuerequisitiontab").bootstrapTable("getSelections")
                      $(cks).each(function (index,item) {
                          $("#Issuerequisitiontab").bootstrapTable("removeByUniqueId",item.productname)
                      });
                  }
            }
        </script>
</body>
</html>
